/**
 * 
 */
package mdb.com.vn.wservices.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import mdb.com.vn.utils.DateUtils;

/**
 * @author tu.dod
 * 
 */
public class WSUtils {
	public static Date stringToDate(String stringdate, String pattern) {
		try {
			return new SimpleDateFormat(pattern).parse(stringdate);
		} catch (ParseException e) {
			return null;
		}

	}

	public static int CalculateLDRFD(String term, double inerest_rate,
			BigDecimal amount) {
		int result = 0;
		if (amount.compareTo(new BigDecimal(30000000)) >= 0 && (term.equals("3M")||term.equals("03M"))
				&& inerest_rate == 6.8) {
			result = amount.divide(new BigDecimal(30000000),BigDecimal.ROUND_FLOOR).intValue();

		}
		if (amount.compareTo(new BigDecimal(30000000)) >= 0 &&(term.equals("6M")||term.equals("06M"))
				&& inerest_rate == 7.5) {
			result = amount.divide(new BigDecimal(30000000),BigDecimal.ROUND_FLOOR).intValue();

		}
		return result;
	}

	public static int CalculateLDRFDCorp(String term, double inerest_rate,
			BigDecimal amount) {
		int result = 0;
		if (amount.compareTo(new BigDecimal(150000000)) >= 0
				&& (term.equals("1M")||term.equals("01M")) && inerest_rate == 6.8) {
			result = amount.divide(new BigDecimal(150000000),BigDecimal.ROUND_FLOOR).intValue();

		}
		if (amount.compareTo(new BigDecimal(100000000)) >= 0
				&& (term.equals("3M")||term.equals("03M"))  && inerest_rate == 6.8) {
			result = amount.divide(new BigDecimal(100000000),BigDecimal.ROUND_FLOOR).intValue();

		}
		if (amount.compareTo(new BigDecimal(100000000)) >= 0
				&& (term.equals("6M")||term.equals("06M")) && inerest_rate == 7.5) {
			result = amount.divide(new BigDecimal(100000000),BigDecimal.ROUND_FLOOR).intValue();

		}
		return result;
	}

	public static int CalculateLDRCasa(int numDate, double inerest_rate,
			BigDecimal amount) {
		int result = 0;
		if (amount.compareTo(new BigDecimal(10000000)) >= 0 && numDate >= 38
				&& inerest_rate == 0.5) {
			BigDecimal temp1 = amount.multiply(new BigDecimal(numDate));
			BigDecimal temp2 = new BigDecimal(10000000)
					.multiply(new BigDecimal(38));
			result = temp1.divide(temp2, BigDecimal.ROUND_FLOOR).intValue();
		}
		return result;
	}

	public static int CalculateLDRFDCasa(double cs_interest_rate,
			double fd_inerest_rate, BigDecimal fDamount, BigDecimal cSAmount,
			String term) {
		int fDresult = 0;
		int cSresult = 0;
		if (fDamount.compareTo(new BigDecimal(20000000)) >= 0
				&& fd_inerest_rate == 6.8 && (term.equals("3M")||term.equals("03M")) ) {

			fDresult = fDamount.divide(new BigDecimal(20000000),
					BigDecimal.ROUND_FLOOR).intValue();
		}
		if (cSAmount.compareTo(new BigDecimal(5000000)) >= 0
				&& cs_interest_rate == 0.5 && (term.equals("3M")||term.equals("03M"))) {

			cSresult = cSAmount.divide(new BigDecimal(5000000),
					BigDecimal.ROUND_FLOOR).intValue();
		}
		
		return 2*(fDresult>cSresult? cSresult:fDresult);
	}

	public static int CalculateGiftBag(int scCard) {
		return scCard / 5;

	}

	public static int CalculateGiftTray(int scCard) {
		return scCard / 10;

	}

	public static int CalculateGiftVoucher(int scCard) {
		return scCard / 30;

	}
	
	public static void lockControl(String term, Date dateLocked, Date endDateLocked){
		if (term.equals("3M")||term.equals("03M")){
			endDateLocked = DateUtils.dateInfuture(dateLocked, 90);
		}
	}

}
